(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ClassdetailCtrl
     * @description
     * # ClassdetailCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ClassdetailCtrl', ClassdetailCtrl);

    ClassdetailCtrl.$inject = ['$scope', 'dataService', 'helper', '$location', '$timeout', 'moment', '$rootScope'];

    function ClassdetailCtrl($scope, dataService, helper, $location, $timeout, moment, $rootScope) {

        $scope.listAction = function() {
            $scope.status = 1;
            $scope.class_id = $location.search().class_id;
            $scope.class_category_id = $location.search().class_category_id;
            $scope.title = $location.search().title;
            $scope.getList();
        }

        //获取列表
        $scope.getList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    store_id: $rootScope.store_id,
                    class_id: $scope.class_id,
                }
            };
            if ($scope.status) {
                query.content.status = $scope.status;
            }

            dataService.classdetailGet(query).then(function(list) {
                if (list && list.class_detail_list) {
                    $scope.class_detail_list = list.class_detail_list;
                    $scope.total = list.total_count;
                }
            });
        }

        $scope.addAction = function() {
            $scope.classdescription = random();
            $scope.class_category_id = $location.search().class_category_id;
            $scope.class_detail_id = $location.search().class_detail_id;
            $scope.title = $location.search().title;
            $scope.class_id = $location.search().class_id;
            if ($scope.class_detail_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            } else {
                $scope.classdetail = { class_tiem: moment().format('YYYY-MM-DD HH:mm:ss') };
                // moment().format('YYYY-MM-DD HH:mm:ss');
            }
        }

        function getOneData() {
            var query = {
                content: {
                    class_detail_id: $scope.class_detail_id,
                    store_id: $rootScope.store_id,
                }
            };
            // console.error("$rootScope", $rootScope);
            dataService.classdetailGet(query).then(function(result) {
                if (result.class_detail_list) {
                    $scope.classdetail = result.class_detail_list[0];
                    $scope.classdetail.desc = $scope.classdetail.desc ? json2html(JSON.parse($scope.classdetail.desc)) : "";
                    // $scope.h5hd.content = $scope.h5hd.content ? json2html(JSON.parse($scope.h5hd.content)) : "";
                }

            });
        }

        //活动添加
        $scope.classAdd = function() {

            if (!$scope.class.total_num) {
                helper.myAlert("课程总数不能为空");
                return false;
            }

            if (!$scope.class.title) {
                helper.myAlert("活动标题不能为空");
                return false;
            }
            var data = {
                content: {
                    "store_id": $rootScope.store_id,
                    "total_num": $scope.class.total_num,
                    "title": $scope.class.title,
                    // "order_id": $scope.order_id,
                }
            }

            if (!$scope.class.class_id) {
                data.content.order_id = $scope.order_id;
            } else {
                data.content.class_id = $scope.class_id;
                data.content.left_num = $scope.class.left_num;
            }

            var method = $scope.class.class_id ? "classUpdate" : "classAdd";

            dataService[method](data).then(function(result) {
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    helper.myAlert("success", "#/classes/list");
                }
            });


        }

        //选择用户回调方法
        $scope.memberdata = function(memberInfo) {
            // console.error("memberInfo", memberInfo);
            $scope.classdetail.member_id = memberInfo.member_id;
        }

        //活动添加
        $scope.classdetailAdd = function() {
            //             console.error("$rootScope", $rootScope);
            if (!$scope.classdetail.class_tiem) {
                helper.myAlert("上课时间不能为空");
                return false;
            }

            var data = {
                content: {
                    "store_id": $rootScope.store_id,
                    "class_tiem": $scope.classdetail.class_tiem,
                    "desc": JSON.stringify(html2json($scope.classdetail.desc)),
                    "status": $scope.classdetail.status,
                    "member_id": $scope.classdetail.member_id,
                    "user_id": $rootScope.currentUser.user_id,
                }
            }

            if ($scope.class_id) {
                data.content.class_id = $scope.class_id;
            }
            if ($scope.class_detail_id) {
                data.content.class_detail_id = $scope.class_detail_id;
            }

            var method = $scope.classdetail.class_detail_id ? "classdetailUpdate" : "classdetailAdd";

            dataService[method](data).then(function(result) {
                if (result.class_detail_id) {
                    helper.myAlert("success", "#/classdetail/list?class_id=" + $scope.class_id + '&class_category_id=' + $scope.class_category_id);
                } else {
                    helper.myAlert("错误：" + result.Msg);
                }
            });


        }



    }
})();